package com.eeepay.eeepay_v2.api;

import com.eeepay.eeepay_v2.model.UserInfo;
import com.eeepay.eeepay_v2.util.Constant;
import com.eeepay.eeepay_v2.util.GrpcUtils;

import java.util.concurrent.TimeUnit;

import cn.eeepay.api.grpc.nano.ManagementCenterProto;
import cn.eeepay.api.grpc.nano.SuperPushServiceGrpc;
import io.grpc.ManagedChannel;

/**
 * 描述：获取超级推分润统计
 * 今日收益统计 or 累计收益
 * 作者：zhuangzeqin
 * 时间: 2017/8/10-13:47
 * 邮箱：zzq@eeepay.cn
 */
public class GetSuperPushShareCountAPI {
    private ResultCallBack resultCallBack;//设置监听接口

    /**
     * 设置监听接口
     *
     * @param resultCallBack
     */
    public void setResultCallBack(ResultCallBack resultCallBack) {
        this.resultCallBack = resultCallBack;
    }
    public GetSuperPushShareCountAPI() {

    }
    /**
     * 获取超级推分润统计  获取 今日收益统计 && 累计收益
     */
    public void getSuperPushShareCountInfo() {
        if (resultCallBack == null) {
            throw new RuntimeException("ResultCallBack is null,you can must implement.");
        }
        GrpcUtils.getInstance().doGrpcTask(Constant.URL.URL, Constant.URL.PORT, 0, new GrpcUtils.AsyncTaskListener() {
            @Override
            public Object doInBack(ManagedChannel channel, int taskId) {
                SuperPushServiceGrpc.SuperPushServiceBlockingStub stub =
                        SuperPushServiceGrpc.newBlockingStub(channel).withDeadlineAfter(30, TimeUnit.SECONDS);
                ManagementCenterProto.UserInfoReq userInfoReq = new ManagementCenterProto.UserInfoReq();
                userInfoReq.userId = UserInfo.getUserInfo2SP().getAgentNo();
                return stub.getSuperPushShareCount(userInfoReq);
            }

            @Override
            public void updateUI(Object result, int taskId) {
                if (result == null) {
                    resultCallBack.onFail("分润统计查询失败,请稍后重试!");
                    return;
                }
                ManagementCenterProto.SuperPushShareCountRes countRes = (ManagementCenterProto.SuperPushShareCountRes)result;
                if (!countRes.resStatus.status)
                {
                    resultCallBack.onFail(countRes.resStatus.msg);
                    return;
                }
                resultCallBack.onSucess(countRes.todayCount,countRes.allCount);

            }
        });
    }

    /**
     * 提供外围接口；通过接口回传
     */
    public interface ResultCallBack {
        void onFail(String msg);//失败返回；提示语

        void onSucess(String todayCount,String allCount);//成功返回数据---今日收益统计 && 累计收益
    }
}
